package cn.spike.user.controller;

import cn.spike.basic.annotation.PreAuthorize;
import cn.spike.basic.exception.BusinessException;
import cn.spike.user.service.IUserService;
import cn.spike.user.domain.User;
import cn.spike.user.query.UserQuery;
import cn.spike.basic.util.AjaxResult;
import cn.spike.basic.util.PageList;
import javafx.scene.chart.BubbleChart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    public IUserService userService;

    /**
     * 接口：添加或修改
     * @param user  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    @PreAuthorize(name = "保存用户",sn="user:save")
    public AjaxResult save(@RequestBody User user){
        try {
            if( user.getId()!=null)
                userService.update(user);
            else
                userService.add(user);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "删除用户",sn="user:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            userService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    @PreAuthorize(name = "用户批量删除",sn="user:patchDel")
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
                userService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    @PreAuthorize(name = "获取用户",sn="user:get")
    public User loadOne(@PathVariable("id")Long id) {
        return userService.loadById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    @PreAuthorize(name = "获取所有用户",sn="user:getAll")
    public List<User> loadAll(){
        return userService.loadAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    @PreAuthorize(name = "分页列表用户",sn="user:list")
    public PageList<User> pageQuery(@RequestBody UserQuery query) {
        return userService.pageQuery(query);
    }


    /**
     * 接口：手机号注册接口
     * @param map
     * @return AjaxResult 响应给前端
     */
    @PostMapping("/register/phone")
    public AjaxResult phoneRegister(@RequestBody Map<String,String> map){
        try {
            userService.phoneRegister(map);
            return new AjaxResult();
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙,请稍后重试");
        }
    }

    /**
     * 接口：邮箱注册接口
     * @param map
     * @return AjaxResult 响应给前端
     */
    @PostMapping("/register/email")
    public AjaxResult emailRegister(@RequestBody Map<String,String> map){
        try {
            userService.emailRegister(map);
            return new AjaxResult();
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙,请稍后重试");
        }
    }

    /**
     * 接口：邮箱激活接口
     * @param logininfoId
     * @return AjaxResult 响应给前端
     */
    @GetMapping("/active/{logininfoId}")
    public String activation(@PathVariable("logininfoId") Long logininfoId){
            userService.activation(logininfoId);
            return "<h1>账号激活成功</h1><h1><a href='http://127.0.0.1:/login.html'>点击前往登录</a></h1>";
    }

}
